package com.tgy.leetcode;

import java.util.HashMap;

/**
 * @Author: tgy
 * @Date: 12/23/20 6:47 PM
 */
public class _387_字符串中的第一个唯一字符 {

    public int firstUniqChar(String s) {

        if (s == null) {
            return -1;
        }


        char[] chars = s.toCharArray();

        if (chars.length == 0) {

            return -1;
        }

        if (s.length() == 1) {

            return 0;
        }

        int[] container = new int[26];

        for (int i = 0; i < chars.length; i++) {

            int index = chars[i] - 'a';
            container[index] += 1;
        }

        int index = -1;

        for (int i = 0; i < chars.length; i++) {

            int idx = chars[i] - 'a';
            if (container[idx] == 1) {

                index = i;
                break;
            }

        }

        return index;
    }
}
